import tornado.ioloop
import tornado.websocket
import asyncio
import time
from multiprocessing import Process
from threading import Thread

class Client:
    def on_message(self, message):
        pass
        # print(f"Server said: {message}")

    async def send_messages(self, count, start_index):
        ws_url = "ws://localhost:8888/ws"
        websocket = await tornado.websocket.websocket_connect(ws_url)
        for i in range(start_index, start_index + count):
            await websocket.write_message(f"Message {i}")
            response = await websocket.read_message()
            if response is None:
                break
            self.on_message(response)
        await websocket.close()

def send_messages_thread(count, start_index):
    asyncio.run(Client().send_messages(count, start_index))

def send_messages_process(count, start_index):
    thread = Thread(target=send_messages_thread, args=(count, start_index))
    thread.start()
    thread.join()

if __name__ == "__main__":
    t = time.time()
    
    processes = []
    total_messages = 100000
    messages_per_process = total_messages // 10
    
    for i in range(10):
        p = Process(target=send_messages_process, args=(messages_per_process, i * messages_per_process))
        processes.append(p)
        p.start()

    for p in processes:
        p.join()

    elapsed_time = time.time() - t
    print(f"Total time taken: {elapsed_time:.2f} seconds")